package com.hotel.controller;

import com.hotel.pojo.GuestRegistration;
import com.hotel.service.GuestRegistrationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/GuestRegistration")
public class GuestRegistrationController {

    @Autowired
    GuestRegistrationServiceImpl GuestRegistrationService;

    @RequestMapping("/add")
    public ModelAndView add(GuestRegistration GuestRegistration){
        ModelAndView mv = new ModelAndView();
        GuestRegistrationService.addGuestRegistration(GuestRegistration);
        mv.setViewName("suc_v");
        return mv;
    }

    @RequestMapping("/delete")
    public String delete(int id){
        GuestRegistrationService.deleteGuestRegistrationById(id);
        return "redirect:/GuestRegistration/list";
    }

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView();
        List<GuestRegistration> GuestRegistrationList=GuestRegistrationService.queryAllGuestRegistration();
        mv.addObject("list",GuestRegistrationList);
        mv.setViewName("GuestRegistration_list");
        return mv;
    }

    @RequestMapping("/update1")
    public ModelAndView update1(int id){
        ModelAndView mv = new ModelAndView();
        GuestRegistration GuestRegistration = GuestRegistrationService.queryGuestRegistrationById(id);
        mv.addObject("v",GuestRegistration);
        mv.setViewName("GuestRegistration_update");
        return mv;
    }

    @RequestMapping("/update2")
    public String update2(GuestRegistration v){
        GuestRegistrationService.updateGuestRegistrationById(v);
        return ("redirect:/GuestRegistration/list");
    }

    @RequestMapping("/find")
    public ModelAndView find(String findByPhone ){
        ModelAndView mv = new ModelAndView();
        GuestRegistration GuestRegistration = GuestRegistrationService.queryGuestRegistrationByPhone(findByPhone);
        List<GuestRegistration> GuestRegistrationList=new ArrayList<GuestRegistration>();
        GuestRegistrationList.add(GuestRegistration);
        if (GuestRegistration==null){
            GuestRegistrationList=GuestRegistrationService.queryAllGuestRegistration();
            mv.addObject("error","未查询出结果");
        }
        mv.addObject("list",GuestRegistrationList);
        mv.setViewName("GuestRegistration_list");
        return mv;
    }
}

